package com.xiaodu.acer.phoneplay.bean.downloadbean;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import org.jetbrains.annotations.NotNull;

import zlc.season.rxdownload3.core.Mission;
import zlc.season.rxdownload3.core.RealMission;
import zlc.season.rxdownload3.database.SQLiteActor;

public class DownloadSqliteActor extends SQLiteActor {
    private String IMG = "img";

    private String ID = "id";        //下载文件的id，必须➕

    private String DISPLAY_NAME = "display";
    private String VERSION_NAME = "version";
    private String PACKAGE_NAME = "package";


    public DownloadSqliteActor(@NotNull Context context) {
        super(context);
    }

    @NotNull
    @Override
    public String provideCreateSql() {
        String formatStr =
                "CREATE TABLE %s (\n" +
                        "%s TEXT PRIMARY KEY NOT NULL,\n" +
                        "%s TEXT NOT NULL,\n" +
                        "%s TEXT,\n" +
                        "%s TEXT,\n" +
                        "%s INTEGER,\n" +
                        "%s TEXT,\n" +
                        "%s TEXT,\n" +
                        "%s INTEGER,\n" +
                        "%s TEXT,\n" +
                        "%s TEXT,\n" +
                        "%s TEXT,\n" +
                        "%s TEXT,\n" +
                        "%s TEXT)";

        return String.format(formatStr, getTABLE_NAME(),
                getTAG(),
                getURL(),
                getSAVE_NAME(),
                getSAVE_PATH(),
                getRANGE_FLAG(),
                getCURRENT_SIZE(),
                getTOTAL_SIZE(),
                getSTATUS_FLAG(),
                DISPLAY_NAME,
                PACKAGE_NAME,
                VERSION_NAME,
                ID,
                IMG
        );
    }

    @NotNull
    @Override
    public ContentValues onCreate(@NotNull RealMission mission) {
        ContentValues cv = super.onCreate(mission);
        if (mission.getActual() instanceof DownloadMission) {
            DownloadMission downloadMission = (DownloadMission) mission.getActual();
            cv.put(DISPLAY_NAME, downloadMission.getDisplayName());
            cv.put(PACKAGE_NAME, downloadMission.getPackageName());
            cv.put(VERSION_NAME, downloadMission.getVersionName());
            cv.put(ID, downloadMission.getId() + "");
            cv.put(IMG, downloadMission.getIcon());
        }
        return cv;
    }


    @NotNull
    @Override
    public Mission onGetAllMission(@NotNull Cursor cursor) {
        Mission mission = super.onGetAllMission(cursor);
        String mDisplayName = cursor.getString(cursor.getColumnIndexOrThrow(DISPLAY_NAME));
        String mVersionName = cursor.getString(cursor.getColumnIndexOrThrow(VERSION_NAME));
        String mPackageName = cursor.getString(cursor.getColumnIndexOrThrow(PACKAGE_NAME));
        String mId = cursor.getString(cursor.getColumnIndexOrThrow(ID));
        String mImg = cursor.getString(cursor.getColumnIndexOrThrow(IMG));
        // 获取数据库的 状态
        int statusFlag = cursor.getInt(cursor.getColumnIndexOrThrow("status_flag"));
        if (mPackageName == null || mPackageName.isEmpty()) {
            mPackageName = "no package";
        }
        if (mVersionName == null || mVersionName.isEmpty()) {
            mVersionName = "no versionName";
        }
        if (mDisplayName == null || mDisplayName.isEmpty()) {
            mDisplayName = "no displayName";
        }
        if (mId == null || mId.isEmpty()) {
            mId = "0";
        }
        if (mImg == null || mImg.isEmpty()) {
            mImg = "no img";
        }

        return new DownloadMission(mission, mDisplayName, mVersionName, mPackageName, Integer.parseInt(mId), mImg, statusFlag);
    }
}
